package com.hz.common.view.loading;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import io.gitee.hongzhenw.R;

public class LoadingView extends LinearLayout {

    private View lineView;
    private LoadingItem loadingItem;
    private TextView loadingMsg;
    private long DURATION_TIME = 500L;
    private float translationYValue = 0f;
    private boolean isStopLoading = false;

    public LoadingView(Context context) {
        this(context, null);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * 图标view
     */
    public LoadingItem getShapeView() {
        return loadingItem;
    }

    /**
     * 分割线view
     */
    public View getLineView() {
        return lineView;
    }

    /**
     * 加载文本
     */
    public TextView getLoadingMsg() {
        return loadingMsg;
    }

    /**
     * 动画时长
     */
    public LoadingView setDurationTime(long time) {
        this.DURATION_TIME = time;
        return this;
    }

    private void init() {
        //添加到当前ViewGroup中
        inflate(getContext(), R.layout.loading_anim, this);
        lineView = findViewById(R.id.line_view);
        loadingItem = findViewById(R.id.shape_view);
        loadingMsg = findViewById(R.id.loading_msg);
        translationYValue = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80f, getResources().getDisplayMetrics());
        post(new Runnable() {
            @Override
            public void run() {
                downAnimation();
            }
        });
    }

    //1.进来就先下落，同时阴影进行缩放，当view绘制完成后开启动画
    public void downAnimation() {
        if (isStopLoading) {
            return;
        }
        //shapeView 下降
        ObjectAnimator shapeViewAnimation = ObjectAnimator.ofFloat(loadingItem, "translationY", 0f, translationYValue);
        //shadowView缩小
        ObjectAnimator shadowViewAnimation = ObjectAnimator.ofFloat(lineView, "scaleX", 1f, 0.3f);
        AnimatorSet downAnimatorSet = new AnimatorSet();
        downAnimatorSet.playTogether(shapeViewAnimation, shadowViewAnimation);
        downAnimatorSet.setDuration(DURATION_TIME);
        downAnimatorSet.start();
        downAnimatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                //监听动画结束后，进行上升操作,同时伴随着shapeView旋转
                upAnimation();
            }
        });
    }

    private void upAnimation() {
        //shapeView上升
        ObjectAnimator shapeViewAnimation = ObjectAnimator.ofFloat(loadingItem, "translationY", translationYValue, 0f);
        //shadowView放大
        ObjectAnimator shadowViewAnimation = ObjectAnimator.ofFloat(lineView, "scaleX", 0.3f, 1f);
        loadingItem.exchangeShape();
        AnimatorSet upAnimatorSet = new AnimatorSet();
        upAnimatorSet.playTogether(shapeViewAnimation, shadowViewAnimation, remoteShapeView());
        upAnimatorSet.setDuration(DURATION_TIME);
        upAnimatorSet.start();
        upAnimatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                //监听动画结束后，进行下降操作
                downAnimation();
            }
        });
    }

    private ObjectAnimator remoteShapeView() {
        switch (loadingItem.getCurrentShape()) {
            case CIRCLE:
            case SQUARE:
                //旋转180度
                return ObjectAnimator.ofFloat(loadingItem, "rotation", 0f, 180f);
            default:
                //旋转120度
                return ObjectAnimator.ofFloat(loadingItem, "rotation", 0f, 120f);
        }
    }

    public void stopAnimation() {
        isStopLoading = true;
        //清除动画
        loadingItem.clearAnimation();
        lineView.clearAnimation();
        if (getParent() != null) {
            //移除当前View
            ViewGroup parent = (ViewGroup) getParent();
            parent.removeView(this);
            removeAllViews();
        }
    }
}
